﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dapper.DBContext;
using Guoc.BigMall.Domain.Entity;
using Guoc.BigMall.Infrastructure.Caching;
using Guoc.BigMall.Web.Services;
using Guoc.BigMall.WebService.Dto;

namespace Guoc.BigMall.Web.Controllers
{
    public class AreaController : Controller
    {
        IDBContext _db;
        ICacheManager _cacheManager;
        public AreaController(IDBContext context, ICacheManager cacheManager)
        {
            _db = context;
            _cacheManager = cacheManager;
        }
        // GET: Area
        public ActionResult List()
        {
            var area =
             _cacheManager.Get<AreaVo>("area", () =>
             {
                 AreaVo areaVo = new AreaVo();
                 var areaList = _db.Table<Area>().ToList();
                 areaVo.Provices = areaList.Where(n => n.Level == 1).ToDictionary(k => k.Id, v => v.Name);
                 areaVo.Citys = areaList.Where(n => n.Level == 2).ToDictionary(k => k.Id, v => v.Name);
                 areaVo.Countys = areaList.Where(n => n.Level == 3).ToDictionary(k => k.Id, v => v.Name);
                 return areaVo;
             });

            return new JsonNetResult(ResultModel.Ok(area));
        }
    }
}